<?php
/**
 * @package YARBB
 * @copyright Авторские права (C) 2009 www.yar-it.com. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * YARBB! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл copyright.php.
 */

// no direct access

defined( '_VALID_MOS' ) or die( '' );

echo '<script type="text/javascript" src="' . $yarbb_live . '/inc/js/jquery.accordion-1.2.1.js"></script>';
if (isset($_POST['cleanup'])) {

	//delete all users and posts from specified ips, then perform all other cleanup tasks except resetting post counts since that might not be needed or wanted.

	@set_time_limit(0);

	$ip = "'".implode("','", array_values(explode(' ', $_POST['ip_addys'])))."'";

	$sql = ('DELETE FROM #__forum_posts WHERE poster_ip IN('.$ip.')');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('DELETE FROM #__forum_users WHERE registration_ip IN('.$ip.')');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_forum_posts SELECT t.forum_id, count(*) as posts FROM #__forum_posts as p LEFT JOIN #__forum_topics as t on p.topic_id=t.id GROUP BY t.forum_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('UPDATE #__forum_forums, #__forum_forum_posts SET num_posts=posts WHERE id=forum_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_forum_topics SELECT forum_id, count(*) as topics FROM #__forum_topics GROUP BY forum_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('UPDATE #__forum_forums, #__forum_forum_topics SET num_topics=topics WHERE id=forum_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_topic_posts SELECT topic_id, count(*)-1 as replies FROM #__forum_posts GROUP BY topic_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('UPDATE #__forum_topics, #__forum_topic_posts SET num_replies=replies WHERE id=topic_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_forum_last SELECT p.posted AS n_last_post, p.id AS n_last_post_id, p.poster AS n_last_poster, t.forum_id FROM #__forum_posts AS p LEFT JOIN #__forum_topics AS t ON p.topic_id=t.id ORDER BY p.posted DESC');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_forum_lastb SELECT * FROM #__forum_forum_last WHERE forum_id > 0 GROUP BY forum_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('UPDATE #__forum_forums, #__forum_forum_lastb SET last_post_id=n_last_post_id, last_post=n_last_post, last_poster=n_last_poster WHERE id=forum_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_topic_last SELECT posted AS n_last_post, id AS n_last_post_id, poster AS n_last_poster, topic_id FROM #__forum_posts ORDER BY posted DESC');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_topic_lastb SELECT * FROM #__forum_topic_last WHERE topic_id > 0 GROUP BY topic_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('UPDATE #__forum_topics, #__forum_topic_lastb SET last_post_id=n_last_post_id, last_post=n_last_post, last_poster=n_last_poster WHERE id=topic_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_orph_topic SELECT t.id as o_id FROM #__forum_topics AS t LEFT JOIN #__forum_posts AS p ON p.topic_id = t.id WHERE p.id IS NULL');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('DELETE #__forum_topics FROM #__forum_topics, #__forum_orph_topic WHERE o_id=id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_orph_posts SELECT p.id as o_id FROM #__forum_posts p LEFT JOIN #__forum_topics t ON p.topic_id=t.id WHERE t.id IS NULL');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('DELETE #__forum_posts FROM #__forum_posts, #__forum_orph_posts WHERE o_id=id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_orph_topics SELECT t.id as o_id FROM #__forum_topics as t LEFT JOIN #__forum_forums as f ON t.forum_id=f.id WHERE f.id is NULL');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('DELETE #__forum_topics FROM #__forum_topics, #__forum_orph_topics WHERE o_id=id');
	$database->setQuery($sql);
	$database->Query($sql);

	mosRedirect('index2.php?option=com_forum&amp;task=forum_cleanup', _Y_CLEANSUS);

}

if (isset($_POST['forum_post_sync'])) {

	// synchronise forum posts

	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_forum_posts SELECT t.forum_id, count(*) as posts FROM #__forum_posts as p LEFT JOIN #__forum_topics as t on p.topic_id=t.id GROUP BY t.forum_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('UPDATE #__forum_forums, #__forum_forum_posts SET num_posts=posts WHERE id=forum_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_forum_topics SELECT forum_id, count(*) as topics FROM #__forum_topics GROUP BY forum_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('UPDATE #__forum_forums, #__forum_forum_topics SET num_topics=topics WHERE id=forum_id');
	$database->setQuery($sql);
	$database->Query($sql);

	mosRedirect('index2.php?option=com_forum&amp;task=forum_cleanup', _Y_CLEANSINHR);

}

elseif (isset($_POST['topic_post_sync'])) {

	// synchronise topic posts

	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_topic_posts SELECT topic_id, count(*)-1 as replies FROM #__forum_posts GROUP BY topic_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('UPDATE #__forum_topics, #__forum_topic_posts SET num_replies=replies WHERE id=topic_id');
	$database->setQuery($sql);
	$database->Query($sql);

	mosRedirect('index2.php?option=com_forum&amp;task=forum_cleanup', _Y_CLEANSINHR);

}

elseif (isset($_POST['user_post_sync'])) {

	// synchronise user posts

	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_user_posts SELECT poster_id, count(*)as posts FROM #__forum_posts GROUP BY poster_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('UPDATE #__forum_users, #__forum_user_posts SET num_posts=posts WHERE id=poster_id');
	$database->setQuery($sql);
	$database->Query($sql);

	mosRedirect('index2.php?option=com_forum&amp;task=forum_cleanup', _Y_CLEANSINHR);

}

elseif (isset($_POST['forum_last_post'])) {

	// synchronise forum last posts

	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_forum_last SELECT p.posted AS n_last_post, p.id AS n_last_post_id, p.poster AS n_last_poster, t.forum_id FROM #__forum_posts AS p LEFT JOIN #__forum_topics AS t ON p.topic_id=t.id ORDER BY p.posted DESC');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_forum_lastb SELECT * FROM #__forum_forum_last WHERE forum_id > 0 GROUP BY forum_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('UPDATE #__forum_forums, #__forum_forum_lastb SET last_post_id=n_last_post_id, last_post=n_last_post, last_poster=n_last_poster WHERE id=forum_id');
	$database->setQuery($sql);
	$database->Query($sql);

	mosRedirect('index2.php?option=com_forum&amp;task=forum_cleanup', _Y_CLEANSINHR);

}

elseif (isset($_POST['topic_last_post'])) {

	// synchronise topic last posts

	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_topic_last SELECT posted AS n_last_post, id AS n_last_post_id, poster AS n_last_poster, topic_id FROM #__forum_posts ORDER BY posted DESC');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_topic_lastb SELECT * FROM #__forum_topic_last WHERE topic_id > 0 GROUP BY topic_id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('UPDATE #__forum_topics, #__forum_topic_lastb SET last_post_id=n_last_post_id, last_post=n_last_post, last_poster=n_last_poster WHERE id=topic_id');
	$database->setQuery($sql);
	$database->Query($sql);

	mosRedirect('index2.php?option=com_forum&amp;task=forum_cleanup', _Y_CLEANSINHR);

}

elseif (isset($_POST['delete_orphans'])) {

	// Clear orphans

	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_orph_topic SELECT t.id as o_id FROM #__forum_topics AS t LEFT JOIN #__forum_posts AS p ON p.topic_id = t.id WHERE p.id IS NULL');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('DELETE #__forum_topics FROM #__forum_topics, #__forum_orph_topic WHERE o_id=id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_orph_posts SELECT p.id as o_id FROM #__forum_posts p LEFT JOIN #__forum_topics t ON p.topic_id=t.id WHERE t.id IS NULL');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('DELETE #__forum_posts FROM #__forum_posts, #__forum_orph_posts WHERE o_id=id');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('CREATE TEMPORARY TABLE IF NOT EXISTS #__forum_orph_topics SELECT t.id as o_id FROM #__forum_topics as t LEFT JOIN #__forum_forums as f ON t.forum_id=f.id WHERE f.id is NULL');
	$database->setQuery($sql);
	$database->Query($sql);
	$sql = ('DELETE #__forum_topics FROM #__forum_topics, #__forum_orph_topics WHERE o_id=id');
	$database->setQuery($sql);
	$database->Query($sql);

	mosRedirect('index2.php?option=com_forum&amp;task=forum_cleanup', _Y_CLEANTOPPOST);

}

else {




	?>

<table class="adminheading">
	<tr>
		<th class="menus"><span><?php echo _Y_CLEAN2; ?></span></th>
	</tr>
</table>
<div id="pageWrap" class="pageWrap">
	<div class="pageContent">
		<ul class="accordion">
			<li> <a href="#clean1"> <?php echo _Y_CLEANSPAM; ?></a>
				<div> <span>
						<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
							<table class="adminlist">
								<tr>
									<th colspan="2"> <?php echo _Y_CLEANSPAM; ?> </th>
								</tr>
								<tr>
									<td><?php echo _Y_CLEANIP; ?></td>
									<td><?php echo mosToolTip(_Y_CLEANIP2); ?>&nbsp;
										<input type="text" name="ip_addys" size="50" maxlength="255" />
										<input type="submit" name="cleanup" value="<?php echo _E_REMOVE; ?>" tabindex="4" />
									</td>
								</tr>
							</table>
						</form>
					</span> </div>
			</li>
			<li> <a href="#clean2"> <?php echo _Y_CLEANTOPPOST2; ?></a>
				<div> <span>
						<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
							<table class="adminlist">
								<tr>
									<th colspan="2"> <?php echo _Y_CLEANTOPPOST2; ?> </th>
								</tr>
								<tr>
									<td><?php echo mosToolTip(_Y_CLEANTOPPOST22); ?>&nbsp;
										<input type="submit" name="forum_post_sync" value="<?php echo _Y_CLEANSIN; ?>" tabindex="4" />
									</td>
								</tr>
							</table>
						</form>
					</span> </div>
			</li>
			<li> <a href="#clean3"> <?php echo _Y_CLEANONPOSTREP; ?></a>
				<div> <span>
						<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
							<table class="adminlist">
								<tr>
									<th colspan="2"> <?php echo _Y_CLEANONPOSTREP; ?> </th>
								</tr>
								<tr>
									<td><?php echo mosToolTip(_Y_CLEANONPOSTREP); ?>&nbsp;
										<input type="submit" name="topic_post_sync" value="<?php echo _Y_CLEANSIN; ?>" tabindex="4" />
									</td>
								</tr>
							</table>
						</form>
					</span> </div>
			</li>
			<li> <a href="#clean4"> <?php echo _Y_CLEANPOSTUSERS; ?></a>
				<div> <span>
						<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
							<table class="adminlist">
								<tr>
									<th colspan="2"> <?php echo _Y_CLEANPOSTUSERS; ?> </th>
								</tr>
								<tr>
									<td><?php echo mosToolTip(_Y_CLEANPOSTUSERS2); ?> &nbsp;
										<input type="submit" name="user_post_sync" value="<?php echo _Y_CLEANSIN; ?>" tabindex="4" />
									</td>
								</tr>
							</table>
						</form>
					</span> </div>
			</li>
			<li> <a href="#clean5"> <?php echo _Y_CLEANLATEST; ?></a>
				<div> <span>
						<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
							<table class="adminlist">
								<tr>
									<th colspan="2"> <?php echo _Y_CLEANLATEST; ?> </th>
								</tr>
								<tr>
									<td><?php echo mosToolTip(_Y_CLEANLATEST2); ?>&nbsp;
										<input type="submit" name="forum_last_post" value="<?php echo _Y_CLEANSIN; ?>" tabindex="4" />
									</td>
								</tr>
							</table>
						</form>
					</span> </div>
			</li>
			<li> <a href="#clean6"> <?php echo _Y_CLEANLATESTTOP; ?></a>
				<div> <span>
						<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
							<table class="adminlist">
								<tr>
									<th colspan="2"> <?php echo _Y_CLEANLATESTTOP; ?> </th>
								</tr>
								<tr>
									<td><?php echo mosToolTip(_Y_CLEANLATESTTOP); ?>&nbsp;
										<input type="submit" name="topic_last_post" value="<?php echo _Y_CLEANSIN; ?>" tabindex="4" />
									</td>
								</tr>
							</table>
						</form>
					</span> </div>
			</li>
			<li> <a href="#clean7"> <?php echo _Y_CLEANTOPNONP; ?></a>
				<div> <span>
						<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
							<table class="adminlist">
								<tr>
									<th colspan="2"> <?php echo _Y_CLEANTOPNONP; ?> </th>
								</tr>
								<tr>
									<td><?php echo mosToolTip(_Y_CLEANTOPNONP2); ?>&nbsp;
										<input type="submit" name="delete_orphans" value="<?php echo _Y_CLEANSIN; ?>" tabindex="4" />
									</td>
								</tr>
							</table>
						</form>
					</span> </div>
			</li>
		</ul>
	</div>
</div>
	<?php
}